package com.heima.leetcode.practice;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @Classname E205
 * @Description leetcode 205. 同构字符串
 * @Date 2025/10/13 17:32
 * @Author 勾新杰
 */
public class E205 {

    /**
     * 方法一：哈希表结合HashSet
     *
     * @param s 字符串1
     * @param t 字符串2
     * @return 是否同构
     */
    public boolean isIsomorphic(String s, String t) {
        HashMap<Character, Character> map = new HashMap<>();
        HashSet<Character> usedValue = new HashSet<>();
        char[] source = s.toCharArray();
        char[] target = t.toCharArray();
        if (source.length != target.length) return false;
        for (int i = 0; i < source.length; i++) {
            if (map.containsKey(source[i])) {
                if (map.get(source[i]) == target[i]) continue;
                else return false;
            } else {
                if (usedValue.contains(target[i])) return false;
                else {
                    map.put(source[i], target[i]);
                    usedValue.add(target[i]);
                }
            }
        }
        return true;
    }
}
